
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

import net.sf.json.JSONObject;

public class MessagePhone {
	// 突发预警 短信推送
	public static final String APPKEY = "23638954";
	public static final String SECRET = "dae7fe4e50349cacb28293993372bab4";
	public static final String TEMPLATE_CODE = "SMS_152212866";
	public static final String SIGN = "突发信息";
	public static final String URL = "http://gw.api.taobao.com/router/rest";

	public static boolean sendMessage(String param, String phoneNumber) {
		boolean ret = true;
		TaobaoClient client = new DefaultTaobaoClient(URL, APPKEY, SECRET);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("");
		req.setSmsType("normal");
		req.setSmsFreeSignName(SIGN);
		req.setSmsParamString(param);
		req.setRecNum(phoneNumber);
		req.setSmsTemplateCode(TEMPLATE_CODE);

		try {
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
			String retJson = rsp.getBody();
			System.err.println(rsp.getBody());
			return checkJson(retJson);
		} catch (Exception e) {

			ret = false;
		}
		return ret;
	}

	private static boolean checkJson(String jsonStr) {
		JSONObject returnObj = JSONObject.fromObject(jsonStr);
		JSONObject result = null;
		if (returnObj.has("alibaba_aliqin_fc_sms_num_send_response")) {
			result = returnObj.getJSONObject("alibaba_aliqin_fc_sms_num_send_response");
			if (!result.getJSONObject("result").getBoolean("success")) {

				return false;
			}
			return true;
		} else if (returnObj.has("error_response")) {

			return false;
		} else
			return false;
	}

	public static void main(String[] args) {
		for (int i = 0; i < 1; i++) {
			JSONObject param = new JSONObject();
			param.accumulate("category", "爆炸");
			param.accumulate("datetime", "凌晨");
			param.accumulate("area", "河北张家口");
			param.accumulate("desc", "一化工厂附近凌晨发生爆炸");
			param.accumulate("level", "高");
			
			sendMessage(param.toString(), "18911758300");
		}

	}

}
